<template>
  <ui-page class="container">
    <!-- table -->
    <ui-table v-bind="table.config" ref="table" class="content_container" :params="{tenantId: userInfo.tenantId}"
              @select-change="onSelectTableEntity">
      <!--   查询   -->
      <template v-slot:search-bar>
        <ui-table-select v-model="table.model.select" :reset="true">
          <ui-table-select-item name="username" label="登录账号" placeholder="请输入登录账号" show-label type="text"
                                :show-word-limit="false"/>
          <ui-table-select-item name="nickname" label="用户昵称" placeholder="请输入用户昵称" show-label type="text"
                                :show-word-limit="false"/>
        </ui-table-select>
      </template>
      <!--   操作   -->
      <template v-slot:tool-bar>
        <el-button type="primary" icon="el-icon-plus" @click="handleEntityCreate">新增</el-button>
        <!--        <el-button type="primary" icon="el-icon-plus" @click="handleEntityDelete">删除</el-button>-->
        <!--        <el-button type="primary" icon="el-icon-printer" @click="handleEntityExport">导出</el-button>-->
      </template>
      <ui-table-column label="序号" type="index" width="60px"/>
      <ui-table-column prop="username" label="登录账号"/>
      <ui-table-column prop="nickname" label="用户昵称"/>
      <ui-table-column prop="roleName" label="角色名称"/>
      <ui-table-column prop="mobile" label="手机号码"/>
      <ui-table-column prop="idCard" label="身份证号"/>
      <ui-table-column prop="createTime" label="创建时间"/>
      <ui-table-column type="action" :buttons="table.buttons" :resizable="false"/>
      <!--   分页   -->
      <template v-slot:footer-bar>
        <ui-table-pagination/>
      </template>
    </ui-table>
    <!-- form -->
    <ui-form-dialog ref="form" :title="form.title" :url="form.url" :loading="entityGetLoading"
                    :visible.sync="form.visible" class="entity-form" @after-submit="afterFormSubmit">
      <ui-form-item name="id" label="ID" type="hidden"/>
      <ui-form-item name="username" label="登录账号" type="text" vtype="required" :disabled="!form.isCreate"/>
      <ui-form-item name="password" label="登录密码" type="password" vtype="required" v-if="form.isCreate"/>
      <ui-form-item name="nickname" label="用户昵称" type="text" vtype="required"/>
      <ui-form-item name="roleId" label="角色名称" type="select" :data="roleList"
                    :props="{ value: 'id', label: 'name' }" vtype="required"/>
      <ui-form-item name="mobile" label="手机号码" type="text" :maxlength="11"/>
      <ui-form-item name="idCard" label="身份证号" type="text" :maxlength="18"/>
    </ui-form-dialog>
  </ui-page>
</template>
<script>
import {mapState} from 'vuex';
import to from "await-to-js";

const SysUser = {
  API: {
    create: '/Sys/SysUser/create',
    update: '/Sys/SysUser/update',
    delete: '/Sys/SysUser/delete',
    select: '/Sys/SysUser/select',
    export: '/Sys/SysUser/export',
    createList: '/Sys/SysUser/createList',
    updateList: '/Sys/SysUser/updateList',
    deleteList: '/Sys/SysUser/deleteList',
    selectList: '/Sys/SysUser/selectList',
    selectPage: '/Sys/SysUser/selectPage',
  },
  data() {
    return {
      api: SysUser.API,
      table: {
        config: {
          url: SysUser.API.selectPage,
        },
        model: {
          select: {},
        },
        buttons: [
          () => ({text: '修改', handle: this.handleEntityUpdateRow}),
          (row) => ({text: '删除', handle: this.handleEntityDeleteRow, disabled: row.id === this.userInfo.id}),
        ],
      },
      form: {
        isCreate: true,
        title: '新增',
        url: SysUser.API.create,
        visible: false,
        rules: {},
      },
      roleList: [],
      tenantList: {},
    };
  },
  computed: {
    ...mapState(['userInfo']),
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    onSelectTableEntity(selects) {
      if (selects.length <= 0) {
        return;
      }
      this.handleEntityUpdateRow(selects[selects.length - 1]);
    },
    handleEntityCreate() {
      this.form.isCreate = true;
      this.$refs.form.resetValues();
      this.form.title = '新增';
      this.form.url = this.api.create;
      this.form.visible = true;
    },
    handleEntityUpdate() {
      const sels = this.$refs.table.getSelection();
      if (sels.length !== 1) {
        this.$message.info('请选择要修改的数据');
        return;
      }
      this.handleEntityUpdateRow(sels[0]);
    },
    handleEntityDelete() {
      const sels = this.$refs.table.getSelection();
      if (sels.length <= 0) {
        this.$message.info('请选择要删除的数据');
        return;
      }
      this.deleteEntities(sels.map(item => item.id)).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    handleEntityUpdateRow(row) {
      this.form.isCreate = false;
      this.form.title = '修改';
      this.form.url = this.api.update;
      this.form.visible = true;

      this.selectEntity(row.id).then(({success, data}) => {
        if (!success) {
          this.form.visible = false;
          return;
        }
        console.log(data)
        this.$refs.form.resetValues(data);
      });
    },
    handleEntityDeleteRow(row) {
      this.deleteEntity(row.id).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    async handleEntityExport() {
      const loading = this.$loading({text: '数据加载中..'});
      const url = this.api.export;
      await this.$refs.table.exportEntity(url, '导出.xls');
      loading.close();
    },
    reloadTable() {
      this.$refs.table && this.$refs.table.reload();
    },
    afterFormSubmit({success}) {
      if (!success) {
        return;
      }
      this.form.visible = false;
      this.reloadTable();
    },
    async getRoleList() {
      const params = this.userInfo.tenantId
      const [, result] = await to(this.$axios.get(`/Sys/SysRole/selectList?tenantId=${params}`))
      this.roleList = result.data || []
      console.log(this.roleList)
    }
  },
};
export default SysUser;
</script>
<style lang="less" scoped>
</style>
